//
//  ViewController2.m
//  Wedding fantastic
//
//  Created by ZLM on 15/6/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AnLiViewController.h"

@interface AnLiViewController ()

@end

@implementation AnLiViewController
-(void)dealloc{
    [_myButtonName release];
    [_strUrl release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    

    self.myTableView.frame = CGRectMake(0, 64+50*kHeight/667, self.view.frame.size.width, self.view.frame.size.height-64-49);
    
    // 回到顶部button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(300 * kWidth / 375, kHeight -  130 * kHeight / 667, 35 * kWidth / 375, 38 * kHeight / 667);
    [button setBackgroundImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateNormal];
    button.alpha = 0.6;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(backTop:) forControlEvents:UIControlEventTouchUpInside];
    self.myPage = 1;
//    [self setupRefresh];
    //上下拉刷新
    
    //网络请求
    [self addHeader];
    [self addFooter];
}
//
- (void)addHeader
{
    __block AnLiViewController *anliVC = self;
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        anliVC.myPage = 0;
        anliVC.Loading = NO;//标记为下拉操作
        [self reCreateData];//重新请求数据
        
    }];
#pragma mark 自动刷新(一进入程序就下拉刷新)
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    __block AnLiViewController *anliVC = self;
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        anliVC.Loading = YES;//标记为上拉操作
        [anliVC reCreateData];//请求更多数据
        
    }];
}
-(void)reCreateData

{
    
    self.myPage++;
    NSString *strURL = [self.strUrl stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",(long)self.myPage]];
    
        //    解析网络数据
        [NetworkTool get:strURL body:BodyHTTP httpResult:JSON success:^(id result) {
    
            //      给案例model赋值
            NSMutableArray *array = result[@"works"];
    
            //        self.model1Array = [NSMutableArray array];
            for (NSDictionary *tempDic in array) {
                TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc] initWithDic:tempDic];
    
                [self.model1Array addObject:mod];
                [mod release];
            }
            //        隐藏大风车
            [self.HUD hide:YES];
            //        刷新数据
            [self.myTableView reloadData];
        }
         //     网络数据获取失败
                 failure:^(NSError *error) {
                     UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                     [alertV show];
    
                 }];
        
    
    [self.myTableView headerEndRefreshing];
    [self.myTableView footerEndRefreshing];
    [self.myTableView reloadData];
    
    
    
}
- (void)backTop:(UIButton *)button
{
    [self.myTableView setContentOffset:CGPointZero animated:YES];
}
//获取按钮名称，并调用网络获取数据方法
-(void)setMyButtonName:(NSString *)myButtonName{
    if (_myButtonName != myButtonName) {
        [_myButtonName release];
        _myButtonName = [myButtonName retain];
    }
    self.navigationItem.title = self.myButtonName;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUD setDimBackground:YES];
    self.HUD.labelText = @"Loading";
    [self.HUD show:YES];
    [self getData];
}
//网络获取数据方法
-(void)getData{
    NSString *urlString = @"";
    
//    判断按钮名称
    
    if ([self.myButtonName isEqualToString:@"婚礼策划"]) {
        
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:0]) objectAtIndex:1]) objectForKey:@"案例"];
        NSLog(@"2222222222222%@",urlString);
        self.strUrl = urlString;
        
    }
    if ([self.myButtonName isEqualToString:@"婚纱摄影"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:1]) objectAtIndex:1]) objectForKey:@"案例"];
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"摄影师"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:2]) objectAtIndex:1]) objectForKey:@"案例"];
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"摄像师"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:3]) objectAtIndex:1]) objectForKey:@"案例"];
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"化妆师"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:4]) objectAtIndex:1]) objectForKey:@"案例"];
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"主持人"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:5]) objectAtIndex:1]) objectForKey:@"案例"];
        self.strUrl = urlString;
    }
    
//    解析网络数据
    [NetworkTool get:urlString body:BodyHTTP httpResult:JSON success:^(id result) {
        
//      给案例model赋值
        NSMutableArray *array = result[@"works"];
        
        self.model1Array = [NSMutableArray array];
        for (NSDictionary *tempDic in array) {
            TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc] initWithDic:tempDic];

            [self.model1Array addObject:mod];
            [mod release];
        }
//        隐藏大风车
        [self.HUD hide:YES];
//        刷新数据
        [self.myTableView reloadData];
    }
//     网络数据获取失败
    failure:^(NSError *error) {
        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertV show];
        
    }];

}
////section标题设置为按钮名称
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    
//    return self.myButtonName;
//    
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    
    //   打开数据库
    [[TaoCanAnLiDataBaseHandle shareDataBase]openDB];
    //    创建表
    [[TaoCanAnLiDataBaseHandle shareDataBase]createTableAnLi];
    //    获取当前内容
    TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc]init];
    mod = [self.model1Array objectAtIndex:indexPath.row];
    mod.name = [mod.merchant objectForKey:@"name"];
    //   内容 插入表中
    [[TaoCanAnLiDataBaseHandle shareDataBase]insertAnLi:mod];
    
    
    
    
    
}

//// 刷新
//- (void)setupRefresh
//{
//    // 下拉刷新
//    [self.myTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    [self.myTableView headerBeginRefreshing];
//    
//    // 上拉加载
//    [self.myTableView addFooterWithTarget:self action:@selector(footerRereshing)];
//    //    [self.myTableView footerBeginRefreshing];
//}
//// 下拉
//- (void)headerRereshing
//{
//    //    [self.goodsCollectionView reloadData];
//    // 刷新结束
//    [self.myTableView headerEndRefreshing];
//}
//// 上拉
//- (void)footerRereshing
//{
//    self.myPage++;
//    NSString *strURL = [self.strUrl stringByReplacingOccurrencesOfString:@"Page=1" withString:[NSString stringWithFormat:@"Page=%ld",(long)self.myPage]];
//    
//    //    解析网络数据
//    [NetworkTool get:strURL body:BodyHTTP httpResult:JSON success:^(id result) {
//        
//        //      给案例model赋值
//        NSMutableArray *array = result[@"works"];
//        
//        //        self.model1Array = [NSMutableArray array];
//        for (NSDictionary *tempDic in array) {
//            TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc] initWithDic:tempDic];
//            
//            [self.model1Array addObject:mod];
//            [mod release];
//        }
//        //        隐藏大风车
//        [self.HUD hide:YES];
//        //        刷新数据
//        [self.myTableView reloadData];
//    }
//     //     网络数据获取失败
//             failure:^(NSError *error) {
//                 UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                 [alertV show];
//                 
//             }];
//    
//    [self.myTableView reloadData];
//    [self.myTableView footerEndRefreshing];
//}

//// 刷新
//- (void)setupRefresh
//{
//    // 下拉刷新
//    [self.myTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    [self.myTableView headerBeginRefreshing];
//    
//    // 上拉加载
//    [self.myTableView addFooterWithTarget:self action:@selector(footerRereshing)];
//    //    [self.myTableView footerBeginRefreshing];
//}
//// 下拉
//- (void)headerRereshing
//{
//    //    [self.goodsCollectionView reloadData];
//    // 刷新结束
//    [self.myTableView headerEndRefreshing];
//}
//// 上拉
//- (void)footerRereshing
//{
//    self.myPage++;
//    NSString *strURL = [self.strUrl stringByReplacingOccurrencesOfString:@"Page=1" withString:[NSString stringWithFormat:@"Page=%ld",self.myPage]];
//    
//    //    解析网络数据
//    [NetworkTool get:strURL body:BodyHTTP httpResult:JSON success:^(id result) {
//        
//        //      给案例model赋值
//        NSMutableArray *array = result[@"works"];
//        
//        //        self.model1Array = [NSMutableArray array];
//        for (NSDictionary *tempDic in array) {
//            TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc] initWithDic:tempDic];
//            
//            [self.model1Array addObject:mod];
//            [mod release];
//        }
//        //        隐藏大风车
//        [self.HUD hide:YES];
//        //        刷新数据
//        [self.myTableView reloadData];
//    }
//     //     网络数据获取失败
//             failure:^(NSError *error) {
//                 UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                 [alertV show];
//                 
//             }];
//    
//    [self.myTableView reloadData];
//    [self.myTableView footerEndRefreshing];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
